from ckeditor.fields import RichTextField
from django.db import models

from user.models import CustomUser


# Create your models here.
class Tag(models.Model):
    title = models.CharField(max_length=100)
    def __str__(self):
        return self.title


class CatagoryForm(models.Model):
    title = models.CharField(max_length=100)
    parent = models.ForeignKey(to='self',on_delete=models.CASCADE, blank=True, null=True)
    def __str__(self):
        return self.title
    def children(self):
        return CatagoryForm.objects.filter(parent=self)


class ArticleModel(models.Model):
    title = models.CharField(max_length=100)
    content = RichTextField(blank=True, null=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    category = models.ManyToManyField(to=CatagoryForm, blank=True)  # 移除了 null=True
    tags = models.ManyToManyField(to=Tag, blank=True)
    author = models.ForeignKey(to=CustomUser, on_delete=models.CASCADE)  # 需要添加 default 值
    state = models.CharField(max_length=100, choices=[('draft', '草稿'), ('published', '发布')], default='draft')

    def __str__(self):
        return self.title

